package com.cisdi.wb.wbtestdataprovider.controller.rocketmq;

import com.alibaba.fastjson.JSON;
import com.cisdi.wb.wbtestdataprovider.bo.DubboLineShutDownBo;
import com.cisdi.wb.wbtestdataprovider.bo.PlanBo;
import com.cisdi.wb.wbtestdataprovider.bo.PlanList;
import com.cisdi.wb.wbtestdataprovider.bo.ShutDownBo;
import com.cisdi.wb.wbtestdataprovider.bo.batchPlan.HeatBo;
import com.cisdi.wb.wbtestdataprovider.bo.batchPlan.RollBatchPlanBo;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class RocketMqController {
    @Autowired
    RocketMQTemplate rocketMQTemplate;

    @PostMapping(value = "/sendPlan")
    public void sendPlan(@RequestBody PlanList planList){
        SimpleDateFormat planTimeFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat matDownTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Map<String,Object>> mapList = new ArrayList<>();
        for (PlanBo planBo: planList.getPlanList()) {
            Map<String,Object> planMap = new HashMap<>();
            planMap.put("planNo",planBo.getPlanNo());
            planMap.put("planTime",matDownTimeFormat.format(new Date()));
            planMap.put("outMatNo",planBo.getOutMatNo());
            planMap.put("planMatSeq",planBo.getPlanMatSeq());
            planMap.put("unitCode",planBo.getUnitCode());
            planMap.put("targetWidth",planBo.getTargetWidth());
            planMap.put("targetWidthMax",planBo.getTargetWidthMax());
            planMap.put("targetWidthMin",planBo.getTargetWidthMin());
            planMap.put("targetThick",planBo.getTargetThick());
            planMap.put("targetThickMax",planBo.getTargetThickMax());
            planMap.put("targetThickMin",planBo.getTargetThickMin());
            planMap.put("Weight",planBo.getWeight());
            planMap.put("planCreator",planBo.getPlanCreator());
            planMap.put("prodCode",planBo.getProdCode());
            planMap.put("steelGrade",planBo.getSteelGrade());
            planMap.put("entryMatFactory",planBo.getEntryMatFactory());
            planMap.put("processCode",planBo.getProcessCode());
            planMap.put("planMatDowntime",matDownTimeFormat.format(new Date()));
            mapList.add(planMap);
        }
        System.out.println(JSON.toJSONString(mapList));
        SendResult sendResult = rocketMQTemplate.syncSend("JH_PRODUCTION_PLAN:test_plan",JSON.toJSONString(mapList));
        System.out.println(sendResult);
//        ZG-SHUTDOWN-SIGNAL-TRACK

    }
//    {
//        "data": {
//        "1481652": 6.9904733,
//                "1481673": 6.947504,
//                "1481698": 7.002558
//    },
//        "deviceId": "10000003",
//            "extraInfo": {},
//        "gatewayId": "4",
//            "gatewayTime": "1705910258566",
//            "msgId": "",
//            "msgKey": "CP",
//            "pushId": "68",
//            "sourceMsgTime": "",
//            "traceId": "240122155738000100014"
//    }
    @PostMapping(value = "/ca2shutdown")
    public void shutdownForCA2(@RequestBody ShutDownBo shutDownBo){
        Map<String,Object> requestMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        Map<String,Object> extraInfoMap = new HashMap<>();
        dataMap.put("1481652",shutDownBo.getRollThree());
        dataMap.put("1481673",shutDownBo.getRollFour());
        dataMap.put("1481698",shutDownBo.getRollFive());
//        dataMap.put("1444063",37.988255);
        requestMap.put("data",dataMap);
        requestMap.put("deviceId","10000003");
        requestMap.put("gatewayId","4");
        requestMap.put("gatewayTime",new Date().getTime());
        requestMap.put("msgId","");
        requestMap.put("msgKey","CP");
        requestMap.put("pushId","68");
        requestMap.put("traceId",shutDownBo.getTraceId());
        requestMap.put("sourceMsgTime","");
        requestMap.put("extraInfo",extraInfoMap);
        System.out.println(JSON.toJSONString(requestMap));
        SendResult sendResult = rocketMQTemplate.syncSend("ZG-SHUTDOWN-SIGNAL-TRACK:test_shutdown",JSON.toJSONString(requestMap));
        System.out.println(sendResult);
        System.out.println(new Date().getTime());
    }

    @PostMapping(value = "/shuangGaoAshutdown")
    public void shutdownForShuangGaoA(@RequestBody DubboLineShutDownBo dubboLineShutDownBo){
        Map<String,Object> requestMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        Map<String,Object> extraInfoMap = new HashMap<>();
        dataMap.put("3229",dubboLineShutDownBo.getHigLineA());
        dataMap.put("3230",dubboLineShutDownBo.getZhajiOne());
//        dataMap.put("1444063",37.988255);
        requestMap.put("data",dataMap);
        requestMap.put("deviceId","10000032");
        requestMap.put("gatewayId","28");
        requestMap.put("gatewayTime",new Date().getTime());
        requestMap.put("msgId","");
        requestMap.put("msgKey","");
        requestMap.put("pushId","103");
        requestMap.put("traceId",dubboLineShutDownBo.getTraceId());
        requestMap.put("sourceMsgTime","");
        requestMap.put("extraInfo",extraInfoMap);
        System.out.println(JSON.toJSONString(requestMap));
        SendResult sendResult = rocketMQTemplate.syncSend("MILL-SHUTDOWN-TRACK:test_shutdown",JSON.toJSONString(requestMap));
        System.out.println(sendResult);
        System.out.println(new Date().getTime());
    }

    @PostMapping(value = "/shuangGaoBshutdown")
    public void shutdownForShuangGaoB(@RequestBody DubboLineShutDownBo dubboLineShutDownBo){
        Map<String,Object> requestMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        Map<String,Object> extraInfoMap = new HashMap<>();
        dataMap.put("3330",dubboLineShutDownBo.getHigLineB());
//        dataMap.put("1444063",37.988255);
        requestMap.put("data",dataMap);
        requestMap.put("deviceId","10000032");
        requestMap.put("gatewayId","28");
        requestMap.put("gatewayTime",new Date().getTime());
        requestMap.put("msgId","");
        requestMap.put("msgKey","");
        requestMap.put("pushId","104");
        requestMap.put("traceId",dubboLineShutDownBo.getTraceId());
        requestMap.put("sourceMsgTime","");
        requestMap.put("extraInfo",extraInfoMap);
        System.out.println(JSON.toJSONString(requestMap));
        SendResult sendResult = rocketMQTemplate.syncSend("MILL-SHUTDOWN-TRACK:test_shutdown",JSON.toJSONString(requestMap));
        System.out.println(sendResult);
        System.out.println(new Date().getTime());
    }
    @PostMapping(value = "/sendBatchPlanForCold")
    public void sendBatchPlanForCold(@RequestBody RollBatchPlanBo rollBatchPlanBo){
        Map<String,Object> requestMap = new HashMap<>();
        Map<String,Object> heatMap = new HashMap<>();
        List<Map<String,Object>> heatList = new ArrayList<>();
        List<Map<String,Object>> billectList = new ArrayList<>();
        heatMap.put("detailedWeight","9.5");
        heatMap.put("heatNo",rollBatchPlanBo.getHeatBoList().get(0).getHeatNo());
        heatMap.put("heatSort","1");
        heatMap.put("steelmakingOrderNo","EN24061400002");
        for (int i=1;i<= rollBatchPlanBo.getRollCount();i++){
            Map<String,Object> billetMap = new HashMap<>();
            String billetNo = rollBatchPlanBo.getHeatBoList().get(0).getHeatNo()+String.format("%03d",i);
            billetMap.put("billetNo",billetNo);
            billetMap.put("billetSingleWeight","4.7");
            billetMap.put("saddleCode","Z0ZG010102");
            billetMap.put("selectCount","1");
            billetMap.put("source","2");
            billetMap.put("steelGradeCode",rollBatchPlanBo.getHeatBoList().get(0).getSteelGradeCode());
            billetMap.put("steelSpecificationCode",rollBatchPlanBo.getSteelSpecificationCode());
            billetMap.put("stockMode","B0");
            billetMap.put("strideCode","Z0ZG01");
            billectList.add(billetMap);
        }
        heatMap.put("detailedNumber",billectList.size());
        heatMap.put("billetList",billectList);
        heatList.add(heatMap);
        requestMap.put("billetMaterialCode","1");
        requestMap.put("createTime",new Date().getTime());
        requestMap.put("erpCode",rollBatchPlanBo.getLineCode());
        requestMap.put("erpName","中型H型钢");
        requestMap.put("heatList",heatList);
        requestMap.put("msc","EHG002");
        requestMap.put("operateFlag",rollBatchPlanBo.getOperateFlag());
        requestMap.put("orderCode",rollBatchPlanBo.getOrderCode());
        requestMap.put("orderUnitAimWt",rollBatchPlanBo.getOrderUnitAimWt());
        requestMap.put("planColdHotFlag",1);
        requestMap.put("plantotalNumber",rollBatchPlanBo.getRollCount());
        requestMap.put("plantotalWeight",9.5);
        requestMap.put("prodCname","热轧H型钢");
        requestMap.put("prodCode",rollBatchPlanBo.getProdCode());
        requestMap.put("prodMidCName","型钢");
        requestMap.put("prodMidCode","E");
        requestMap.put("psc","EHG05235001");
        requestMap.put("remark","4-6");
        requestMap.put("rollBatchNo",rollBatchPlanBo.getRollBatchNo());
        requestMap.put("rollCode",rollBatchPlanBo.getRollCode());
        requestMap.put("sendTime",new Date().getTime());
        requestMap.put("sgStd","GB/T11263-2017");
        requestMap.put("specificationCode",rollBatchPlanBo.getSpecificationCode());
        requestMap.put("subSteelGrade",rollBatchPlanBo.getSubSteelGrade());
        requestMap.put("uniqueKey",rollBatchPlanBo.getRollBatchNo());
        requestMap.put("sizingLength",rollBatchPlanBo.getSizingLength());
        System.out.println(requestMap);
        SendResult sendResult = rocketMQTemplate.syncSend("SC-PRODUCTION_OPERATION-ROLL_BATCH_PLAN:test_batchPlan",JSON.toJSONString(requestMap));
        System.out.println(sendResult);

    }
}
